package com.freedy.dataStructure.stack;

/**
 * @author Freedy
 * @date 2021/3/16 16:34
 */
public class ArrStack<T> {
    private final int maxSize;//栈的大小
    private final T[] stack;//数组模拟栈
    private int top=-1;//表示栈顶

    public int size(){
        return top+1;
    }

    public ArrStack(int maxSize){
        this.maxSize=maxSize;
        stack=(T[])new Object[this.maxSize];
    }

    /**
     * 判断栈是否为满
     */
    public boolean isFull(){
        return top==maxSize-1;
    }

    /**
     * 判断栈是否为空
     */
    public boolean isEmpty(){
        return top==-1;
    }

    /**
     * 入栈
     */
    public void push(T val){
        if (isFull()) throw new RuntimeException("栈满");
        top++;
        stack[top]=val;
    }

    /**
     * 出栈
     */
    public T pop(){
        if (isEmpty()) throw new RuntimeException("栈空");
        top--;
        return stack[top+1];
    }

    /**
     * 栈的遍历
     */
    public void list(){
        if (isEmpty()) throw new RuntimeException("栈空");
        for (int i = top; i >=0; i--) {
            System.out.println(stack[i]);
        }
    }

    public T peak(){
        return stack[top];
    }

}
